<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ForeachController extends Controller
{
    public function getList(Request $request)
    {
        $max = $request->input('max');
        $min = $request->input('min');
        //接收数据
        $title = $request->input('GoodsName');
        //查询数据库
        $data = DB::table('goods')->where('goods_name','like','%'.$title.'%')->get();
        $num = count($data);
        if($data){
            session(['data'=>'true']);
            return view('home.foreach.foreach',['num'=>$num,'title'=>$title,'goodsAll'=>$data,'min'=>$min,'max'=>$max]);
        }else{
            session()->forget('data');
            return view('home.foreach.foreach',['title'=>$title]);
        }
    }

    //价格
    public function postPrice(Request $request)
    {
        $min = $request->input('min');
        $max = $request->input('max');
        $title = $request->input('GoodsName');
        //查询数据
        if(is_numeric($min) && is_numeric($max)){
            $res = DB::table('goods')
            ->where('goods_name','like','%'.$title.'%')
            ->whereBetween('price',[$min,$max])
            ->get();
        }else{
            $res = DB::table('goods')
            ->where('goods_name','like','%'.$title.'%')
            ->get();
        }

        //数量
        $num = count($res);
         if($res){
                session(['data'=>'true']);
                return view('home.foreach.foreach',['min'=>$min,'max'=>$max,'goodsAll'=>$res,'title'=>$title,'num'=>$num]);
            }else{
                session()->forget('data');
                return view('home.foreach.foreach',['min'=>$min,'max'=>$max,'title'=>$title]);
            }
    }
}
